<?php

declare(strict_types=1);

namespace App\CustHub\Service;

use App\CustHub\Mapper\ChCaseMapper;
use App\CustHub\Model\ChCaseIndustry;
use Mine\Abstracts\AbstractService;
use Mine\Exception\NormalStatusException;
use Mine\MineModel;


class ChCaseService extends AbstractService
{
    public $mapper;

    public function __construct(ChCaseMapper  $mapper)
    {
        $this->mapper = $mapper;
    }

    /**
     * 获取客户案例分类列表
     * @return array
     */
    public function getCaseIndustry(): array
    {
        $allData = ChCaseIndustry::query()->get();

        $industryZeroData = $allData->filter(function ($item) {
            return $item->industry === 0;
        });

        $industryNonZeroData = $allData->filter(function ($item) {
            return $item->industry !== 0;
        });

        $industryNonZeroData = $industryNonZeroData->sortBy('industry');

        $sortedData = $industryNonZeroData->concat($industryZeroData);

        return $sortedData->toArray();
    }

    public function CaseIndustry($params): array
    {
        $query = ChCaseIndustry::query()
            ->select('*');
        if (isset($params['pageSize'])) {
            $filters=[
                'content'=>'like',
            ];

            $query = applyFilters($query, $params,$filters);
            $paginate = $query->paginate((int)$params['pageSize']??MineModel::PAGE_SIZE);
            $result =  $this->mapper->setPaginate($paginate);
        }
        else{
            $result = $query->get()->toArray();
        }
        return $result;
    }

    /**
     * 获取客户案例
     * @param $params
     * @return array
     */
    public function getCaseList($params): array
    {
        return $this->mapper->getCaseList($params);
    }

    /**
     * 根据行业获取客户案例列表
     * @param $params
     * @return array
     */
    public function getCaseListByIndustry($params): array
    {
        return $this->mapper->getCaseListByIndustry($params);
    }

    /**
     * 获取客户案例详情
     * @param $params
     * @return \Hyperf\Database\Model\Collection
     */
    public function getCaseDetail($params)
    {
        return $this->mapper->getCaseDetail($params);
    }

    public function save(array $data):mixed
    {
        return $this->mapper->save($data);
    }

    public function update(mixed $id, array $data): bool
    {
        return $this->mapper->update($id, $data);
    }

    public function delete(array $ids): bool
    {
        return $this->mapper->delete($ids);
    }

    public function addIndustry(array $params)
    {
        if ($params['content'] === '其他') {
            $newIndustry = 0;
        } else {
            $existingIndustries = ChCaseIndustry::pluck('industry')->toArray();
            $newIndustry = 1;
            while (in_array($newIndustry, $existingIndustries)) {
                $newIndustry++;
            }
        }

        $data = [];
        $data['industry'] = $newIndustry;
        $data['content'] = $params['content'];
        $result = ChCaseIndustry::create($data);
        return $result->industry;
    }

    public function updateIndustry(mixed $industry, array $data)
    {
        ChCaseIndustry::query()->where('industry', $industry)->update($data);
        return true;
    }

    public function deleteIndustry($industries): bool
    {
        $delete = ChCaseIndustry::query()
            ->whereIn('industry', $industries)
            ->delete();
        if (!$delete) {
            throw new NormalStatusException("删除失败");
        }
        return true;
    }
}